package com.usian.controller;


import com.usian.pojo.Staff;
import com.usian.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private CompanyService companyService;

    @RequestMapping("loginto")
    public String login(){
        return "login";
    }


    @RequestMapping("/info")
    public String info(Staff staff, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        String sname = staff.getSname();
        Staff findstaff = companyService.findName(sname);
        if (findstaff!=null){
            if (findstaff.getPsw().equals(staff.getPsw())){
                if (findstaff.getRid()==1){
                    String username = request.getParameter("sname");
                    session.setAttribute("username",username);
                    return "redirect:/company/findAll";
                }
                if (findstaff.getRid()==2){
                    String username = request.getParameter("sname");
                    session.setAttribute("username",username);
                    return "redirect:/company/findAll2";
                }
            }
            return "redirect:/login/loginto";
        }
        return "redirect:/login/loginto";
    }

}
